﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web_HengliSeed.en_us
{
    public partial class GuestBook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData("");
            }
        }

        private void BindData(string strClass)
        {
            int DataCount = 0;
            int NowPage = 1;
            int AllPage = 0;
            int PageSize = Convert.ToInt32(HPageSize.Value);
            switch (strClass)
            {
                case "next":
                    NowPage = Convert.ToInt32(HNowPage.Value) + 1;
                    break;
                case "up":
                    NowPage = Convert.ToInt32(HNowPage.Value) - 1;
                    break;
                case "end":
                    NowPage = Convert.ToInt32(HAllPage.Value);
                    break;
                default:
                    break;
            }
            DataTable dsLog = BLL.GuestBook.GetList(NowPage, PageSize, out AllPage, out DataCount, string.Empty);
            if (dsLog.Rows.Count == 0 || AllPage == 1)
            {
                LBEnd.Enabled = false;
                LBHome.Enabled = false;
                LBNext.Enabled = false;
                LBUp.Enabled = false;
            }
            else if (NowPage == 1)
            {
                LBHome.Enabled = false;
                LBUp.Enabled = false;
                LBNext.Enabled = true;
                LBEnd.Enabled = true;
            }
            else if (NowPage == AllPage)
            {
                LBHome.Enabled = true;
                LBUp.Enabled = true;
                LBNext.Enabled = false;
                LBEnd.Enabled = false;
            }
            else
            {
                LBEnd.Enabled = true;
                LBHome.Enabled = true;
                LBNext.Enabled = true;
                LBUp.Enabled = true;
            }
            rptData.DataSource = dsLog;
            rptData.DataBind();
            if (dsLog.Rows.Count > 0)
            {
                //PageMes.Text = string.Format("[每页<font color=green>{0}</font>条 第<font color=red>{1}</font>页／共<font color=green>{2}</font>页   共<font color=green>{3}</font>条]", PageSize, NowPage, AllPage, DataCount);
                LBEnd.Visible = true;
                LBHome.Visible = true;
                LBNext.Visible = true;
                LBUp.Visible = true;
            }
            else
            {
                PageMes.Text = "";
                LBEnd.Visible = false;
                LBHome.Visible = false;
                LBNext.Visible = false;
                LBUp.Visible = false;
            }
            HNowPage.Value = Convert.ToString(NowPage++);
            HAllPage.Value = AllPage.ToString();

        }

        protected void LBHome_Click(object sender, EventArgs e)
        {
            BindData("");
        }

        protected void LBUp_Click(object sender, EventArgs e)
        {
            BindData("up");
        }

        protected void LBNext_Click(object sender, EventArgs e)
        {
            BindData("next");
        }

        protected void LBEnd_Click(object sender, EventArgs e)
        {
            BindData("end");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string code = Session["CheckCode"].ToString();
            if (!txtCode.Text.Equals(code))
            {
                Common.ShowMessage.Show(Page, "Error", "验证码错误");
                return;
            }
            if (Web_HengliSeed.BLL.GuestBook.Insert(txtUserName.Text.Trim(), txtEmail.Text.Trim(), txtPhone.Text.Trim(), txtContent.Text.Trim(), ""))
            {
                Common.ShowMessage.Show(Page, "Success", "发送成功");
                txtCode.Text = string.Empty;
                txtContent.Text = string.Empty;
                txtEmail.Text = string.Empty;
                txtPhone.Text = string.Empty;
                txtUserName.Text = string.Empty;

                BindData("");
            }
            else
            {
                Common.ShowMessage.Show(Page, "Error", "发送失败");
            }
        }
    }
}